<?php

namespace App\Http\Controllers;

use App\Models\CreditsExchangeRule;
use App\Models\Mgift;
use App\Repositories\OrderRepository;

class OrderController extends Controller
{
    /**
     * 订单列表
     * @param OrderRepository $repository
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function orderList(OrderRepository $repository)
    {
        $orders = $repository->getOrderDataByCardNo();

        return view('order.order_list',compact('orders'));
    }

    /**
     * 购物小票
     * @param $billno
     * @param OrderRepository $repository
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function receipt($billno,OrderRepository $repository)
    {
        $sale_info = $repository->getReceiptByBillno($billno);

        if(!$sale_info) abort(403,'未找到小票信息');

        $sale_info = $sale_info->toArray();

        //if($saleInfo->custno != $this->getSeesionWxuseMcard()->card_no) abort(403,'您无权查看该小票信息');
        $details = explode('|',$sale_info['detail']);
        $should_pay = 0;
        $goods_count = 0;
        foreach ($details as $value){
            $arr_detail[] = explode(',',$value);
            if(isset($value[7]) && $value[7]){
                $should_pay += $value[7];
            }
            if(isset($value[6]) && $value[6]){
                $goods_count += $value[6];
            }
        }

        $sale_info['detail'] = $arr_detail;
        $sale_info['saledate'] = substr($sale_info['saledate'],0,10);
        $sale_info['invno'] = str_pad($sale_info['invno'],8,0,STR_PAD_LEFT);
        //获取条形码号 收银机+小票号+校验位
        $sale_info['reeipt_no'] = $sale_info['syjid'].$sale_info['invno'].receiptSign($sale_info['syjid'].$sale_info['invno']);
        $sale_info['payment'] = explode(',',$sale_info['payment']);

        return view('order.receipt',compact('sale_info','should_pay','goods_count'));
    }

    /**
     * 积分兑换
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function creditExchange()
    {
        $gifts = CreditsExchangeRule::getActiveRuleByMerchantId(session('web_merchant')->id_merchant)->toArray();

        return view('order.credit_exchange',compact('gifts'));
    }

    //兑换记录
    public function exchangeRecode()
    {
        $exchange_records = $this->getSeesionWxuseMcard()->exchangeRecord()->orderBy('created_at','desc')->get();
//        $mycard = $this->getSeesionUser()->wxuserMcard()->select('remain_credits')->first();
        if(empty($exchange_records->toArray())){
            $data=[];
        }else{
            $data = $exchange_records->groupBy(function($item){
                return $item->created_at->year.'年'.$item->created_at->month.'月';
            });
        }

        return view('order.credits_exchange_recode',compact('data'));
    }

    /**
     * 余额充值
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function rechargeBalance()
    {
        return view('order.recharge_balance');
    }

    /**
     * 礼品卡区
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function gift(OrderRepository $repository)
    {
        //可购买的礼品卡
        $gifts = $repository->getOnlineMgifCardByMerchant();
        if(empty($gifts->toArray())) $gifts = [];

        //已购买的礼品卡
        $buy_gifts = $repository->getGiftCardByBuyUserId();

        $bind = [];
        $unbind = [];
        foreach ($buy_gifts as $gift){
            if($gift->use_id_wxuser_mcard && $gift->use_id_wxuser_mcard == $this->getSeesionWxuseMcard()->id_wxuser_mcard){
                $bind[] = $gift;
            }else{
                $unbind[] = $gift;
            }
        }

        return view('order.gift',compact('gifts','bind','unbind'));
    }

    //购买礼平卡页面
    public function buyGiftCard($id,OrderRepository $repository)
    {
        $gift_card = $repository->getGiftCardById($id);

        if(empty($gift_card)) return abort('403','未找到礼平卡信息');

        $can_user_balance = false;
        if($gift_card->cash<=$this->getSeesionWxuseMcard()->remain_cash) $can_user_balance = true;

        return view('order.buy_giftcard',compact('gift_card','can_user_balance'));
    }

    //绑卡记录
    public function giftRecord(OrderRepository $repository)
    {
        $record = $repository->getUserBindGiftCard();

        $gift_record = $record->groupBy(function($item){
            return substr($item->use_date_time,0,4).'年'.substr($item->use_date_time,5,2).'月';
        });

        $sum_cash = sprintf("%.2f",$record->sum('pay_amount')/100);

        if(empty($gift_record->toArray())) $gift_record=[];

        return view('order.gift_record',compact('sum_cash','gift_record'));
    }
}
